<?php
/* MAKING DABR INVITE ONLY - BY davidcarrington & NetPuter */

header('Content-Type: text/html; charset=utf-8');
require_once 'config.php';

$whitelist_file = 'allowed_users.txt';

function config_log_request() {
    if (!user_is_authenticated()) return;

    global $whitelist_file;
    $allowed_users = file($whitelist_file);
    if (!in_array(strtolower(user_current_username())."\n", $allowed_users)) {
        user_logout();
        die("对不起，您不是受邀用户，无法登录。");
    }
}

$pwd = INVITE_CODE;

if (isset($_GET['p']) && isset($_GET['u'])) {
    if ($_GET['p'] == $pwd) {
        $user = strtolower($_GET['u']);

        $line = $user."\n";
        $allowed_users = file($whitelist_file);
        if (in_array($line, $allowed_users)) {
            echo "$user 已经在受邀用户列表！";
            exit;
        }
        
        if (!is_writable($whitelist_file)) {
            echo "受邀用户列表不可写。";
            exit;
        }

        $handle = fopen($whitelist_file, 'a');

        if (!$handle) {
            echo "不能打开受邀用户列表。";
            exit;
        }

        if (fwrite($handle, $line) == FALSE) {
            echo "不能写入到受邀用户列表。";
            exit;
        }

        echo "成功地将 $user 加入到受邀用户列表！";
        fclose($handle);
    } else {
        echo "邀请密码错误！";
    }
}
?>